package cn.wolfcode.trip.app.Filter;

import cn.wolfcode.trip.base.util.UploadUtil;
import org.apache.commons.io.FileUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

public class ImagesFilter implements Filter{
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) servletRequest;
        String uri = req.getRequestURI();
        File file =new File(UploadUtil.UPLOADPATH+uri);
        if (file.exists()) {
            response.getOutputStream().write(FileUtils.readFileToByteArray(file));
        }else {
            filterChain.doFilter(req,response);
        }
    }

    @Override
    public void destroy() {

    }
}
